//
//  DIYScanViewController.m
//  LBXScanDemo
//
//  Created by lbxia on 2017/6/5.
//  Copyright © 2017年 lbx. All rights reserved.
//

#import "DIYScanViewController.h"
#import <Photos/Photos.h>
#import "ScenicDescribeVC.h"
#import "PlayerItem.h"

typedef NS_ENUM(NSUInteger, ScanType) {
    ScanTypeScenic,
};

@interface DIYScanViewController ()

@end

@implementation DIYScanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = MYLocalizedString(@"SCAN_TITLE", nil);
    
    self.cameraInvokeMsg = MYLocalizedString(@"SCAN_LOADING", nil);
    self.isOpenInterestRect = YES;
    self.libraryType = SLT_Native;
    self.scanCodeType = SCT_QRCode;
    
    self.style = [self InnerStyle];
    
    [self setupNav];
    
    [self getAuthorizationStatus];
    
//    相册功能
//    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:MYLocalizedString(@"MYINFO_PHOTO", nil) style:UIBarButtonItemStylePlain target:self action:@selector(openPhoto)];
//    barItem.tintColor = [UIColor blackColor];
//    self.navigationItem.rightBarButtonItem = barItem;
    
    [[PlayerItem playItemShareManager] hide];
}


- (UIBarButtonItem *)rt_customBackItemWithTarget:(id)target action:(SEL)action {
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    return item;
}

- (void)goBack {
    
    [self.navigationController popViewControllerAnimated:YES];
    [[PlayerItem playItemShareManager] show];
}

- (void)openPhoto {
    
    [self openLocalPhoto:YES];
}

- (void)getAuthorizationStatus {
    
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    
    if (authStatus == AVAuthorizationStatusDenied) {
        
        [IGAlertView alertWithTitle:@"" message:MYLocalizedString(@"PUBLIC_CAMERA_ALERT_CONTENT", nil) cancelButtonTitle:MYLocalizedString(@"PUBLIC_CANCEL", nil) commitBtn:MYLocalizedString(@"PUBLIC_NOGPS_ALERT_COMMIT", nil) commit:^{
            
            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [[UIApplication sharedApplication] openURL:url];
        } cancel:nil];
    }
}

#pragma mark -无边框，内嵌4个角
- (LBXScanViewStyle*)InnerStyle
{
    //设置扫码区域参数
    LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
    style.centerUpOffset = 44;
    style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_Inner;
    style.photoframeLineW = 3;
    style.photoframeAngleW = 18;
    style.photoframeAngleH = 18;
    style.isNeedShowRetangle = NO;
    
    style.anmiationStyle = LBXScanViewAnimationStyle_LineMove;
    style.colorAngle = kThemeColor;
    
    //qq里面的线条图片
    UIImage *imgLine = [UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_light_green"];
    style.animationImage = imgLine;
    
    return style;
}

#pragma mark -实现类继承该方法，作出对应处理

- (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array {
    
    [self stopScan];

    if (!array ||  array.count < 1) {

        NSInteger time = [IGUtil showToast:MYLocalizedString(@"INVALID_CODE", nil)];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self reStartDevice];
        });

        return;
    }

    LBXScanResult *scanResult = array[0];

    NSString *strResult = scanResult.strScanned;


    NSData *jsonData = [strResult dataUsingEncoding:NSUTF8StringEncoding];

    NSError *err;

    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];

    if (![dict containsObjectForKey:@"type"] || ![dict containsObjectForKey:@"code"]) {

        NSInteger time = [IGUtil showToast:MYLocalizedString(@"INVALID_CODE", nil)];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self reStartDevice];
        });

        return;
    }
    
    @weakify(self);
    [NetAPIManager scanWithParams:dict block:^(id result, NSError *error) {
        @strongify(self);
        
        if (!error) {

            [self goBack];
            
            if ([_myDelegate respondsToSelector:@selector(scanSuccess:)]) {
                [_myDelegate scanSuccess:result];
            }
            
            return ;
        }
        
        [IGAlertView alertWithTitle:MYLocalizedString(@"PUBLIC_ALERT", nil) message:[IGUtil tipFromError:error] cancelButtonTitle: MYLocalizedString(@"PUBLIC_CONFIRM", nil) commitBtn:nil commit:nil cancel:^{
            
            [self reStartDevice];
        }];
    }];
}

@end


